﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WCFMINERP.BLL.PhotoDownload;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.Services.Files;

namespace WCFMINERP.FileHosting
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        ServiceHost filesHost = new ServiceHost(typeof(FilesService));  //提供下载图片的服务


        PhotoDownloadBll loadBll = new PhotoDownloadBll();

        int Time = 10 * 1000; //间隔时间

        private void Start()
        {
            WriteAnalyseLog.WriteLogFile(DateTime.Now + "：启动程序");
            filesHost.Open();  //打开下载服务
        }

        private void Main_Load(object sender, EventArgs e)
        {
            date_Start.Text = DateTime.Now.AddDays(-3).ToString();
            date_End.Text = DateTime.Now.AddDays(1).ToString();

            System.Threading.Thread threaFilesHost = new System.Threading.Thread(Start);
            threaFilesHost.Start();    

            //System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            //timer.Interval = Time;
            //timer.Tick += timer_Tick;
            //timer.Start();
        
            System.Threading.Thread threa = new System.Threading.Thread(timer_Tick);
            threa.Start();


            //System.Windows.Forms.Timer timerDele = new System.Windows.Forms.Timer();
            //timerDele.Interval = 1000 * 5;// 1000 * 60 * 60 * 2; //2小时执行一次
            //timerDele.Tick += timerDele_Tick;
            //timerDele.Start();

            System.Threading.Thread threaDelete = new System.Threading.Thread(timerDele_Tick);
            threaDelete.Start();

            gridViewDownLoad.OptionsSelection.MultiSelect = true;
            //gridViewDownLoad.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
        }


        bool isDele = false;
        /// <summary>
        /// 删除已经过时的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timerDele_Tick()
        {
            while(!isDele)
            {
                isDele = !isDele;
                int Number = loadBll.deleteDownData();
                if (Number > 0)
                {
                    WriteAnalyseLog.WriteLogFile(DateTime.Now + ":删除下载列表" + Number + "条数据");
                }
                isDele = !isDele;
                Thread.Sleep(50000);
            }
        }
        
        bool isRun = false;
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick()
        {
            while(!isRun)
            {
                isRun = !isRun;
                //WriteAnalyseLog.WriteLogFile(DateTime.Now + ":向下载列表开始添加数据");
                int Number = loadBll.implementMethod();
                if (Number > 0)
                {
                    WriteAnalyseLog.WriteLogFile(DateTime.Now + ":向下载列表添加" + Number + "条数据");
                }
                //WriteAnalyseLog.WriteLogFile(DateTime.Now + ":向下载列表添加结束");
                isRun = !isRun;
                Thread.Sleep(5000);
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Serch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(date_Start.Text) && !string.IsNullOrEmpty(date_End.Text))
            {
                List<CusFileInfo> DataList = loadBll.getFileInfoList(Convert.ToDateTime(date_Start.Text), Convert.ToDateTime(date_End.Text));
                gridDownLoad.DataSource = DataList;
            }
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Add_Click(object sender, EventArgs e)
        {            
            frmSelectFile file = new frmSelectFile();
            DialogResult = file.ShowDialog();
            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                but_Serch_Click(null, null);
            }
        }

        /// <summary>
        /// 移除数据库文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Remove_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("确定删除？", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            for (int i = 0; i < gridViewDownLoad.RowCount; i++)
            {
                CusFileInfo model = gridViewDownLoad.GetRow(i) as CusFileInfo;
                if (model.check)
                {
                    if (System.IO.File.Exists(model.filepath))
                    {
                        //System.IO.File.Delete(model.filepath);  //删除文件
                    }
                    loadBll.DeleteDownData(model.filepath,model.outshopid);
                }
            }

            but_Serch_Click(null, null);
        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {          
            this.Dispose();
            Application.Exit();
            Environment.Exit(-1);
        }

    }
}
